package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.SupplierDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.*;
import com.atguigu.jxc.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SupplierServiceImpl implements SupplierService {

    @Autowired
    private SupplierDao supplierDao;

    @Override
    public Map<String, Object> list(Integer page,
                                    Integer rows,
                                    String supplierName) {
        //获取库存
        Map<String, Object> map = new HashMap<>();
        page = page == 0 ? 1 : page;
        int offSet = (page - 1) * rows;
        List<Supplier> supplierList = supplierDao.selectSupplierList(offSet, rows, supplierName);

        map.put("total", String.valueOf(supplierList.size()));
        map.put("rows", supplierList);
        return map;
    }

    @Override
    public ServiceVO saveOrUpdate(Supplier supplier, String supplierId) {

        if (supplierId == null) {
            //如果供应商id为空添加数据
            this.supplierDao.saveSupplier(supplier);
        } else
            this.supplierDao.updateSupplier(supplier, supplierId);
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    @Override
    public void delete(int id) {
        supplierDao.deleteSupplier(id);
    }

    @Override
    public List<Supplier> getComboboxList(String q) {
        List<Supplier> comboboxList = supplierDao.getComboboxList(q);
        List<Supplier> collect = comboboxList.stream().filter(supplier -> {
            return supplier.getSupplierId().intValue() != 0;
        }).collect(Collectors.toList());
      return collect;
    }
}


